package assdraw2.statusagent;

import assdraw2.canvas.abstraction.Shape;
import assdraw2.common.MessageType;
import assdraw2.common.PACAgent;
import assdraw2.common.PACMessage;
import assdraw2.statusagent.abstraction.StatusBarAbstraction;
import assdraw2.statusagent.presentation.StatusBarPresentation;

public class StatusBarControl implements PACAgent {
	
	private PACAgent parent;
	private StatusBarPresentation presentation;
	private StatusBarAbstraction abstraction;
	
	public StatusBarControl(PACAgent parent){
		super();
		this.parent = parent;		
	}
	


	@Override
	public void receiveMessage(PACMessage m) {
		if(m.getType() == MessageType.OPEN){
			abstraction = new StatusBarAbstraction(); 				
			presentation = new StatusBarPresentation(this);
			return;
		}else if(m.getType() == MessageType.CLOSE){
			abstraction = null;
			presentation.dispose();
			return;
		}
		
		if(abstraction == null){
			return;
		}
		else if(m.getType() == MessageType.SHAPE_ADDED){
			abstraction.setStatus("Shape " + m.getContent().toString() + " added at " + ((Shape)m.getContent()).getX() +","+((Shape)m.getContent()).getY());
		}else if(m.getType() == MessageType.SHAPE_DELETED){
			abstraction.setStatus("Shape(s) deleted.");
		}else if(m.getType() == MessageType.SHAPE_MOVED){
			abstraction.setStatus("Shape "+ m.getContent().toString()+ " Z order changed.");
		}else if(m.getType() == MessageType.SHAPE_SELECTION){
			abstraction.setStatus("Shape "+ m.getContent().toString()+" added to selection.");
		}else if(m.getType() == MessageType.SHAPE_UNGROUP){				
			abstraction.setStatus("Selected shapes ungrouped.");
		}else if(m.getType() == MessageType.SHAPE_GROUP){				
			abstraction.setStatus("Selected shapes grouped.");
		}
		presentation.updateStatus();

	}

	@Override
	public void sendMessage(PACMessage m) {
		if(m.getType() == MessageType.CLOSE){
			parent.receiveMessage(m);
		}

	}

	public String getStatus() {		
		return abstraction.getStatus();
	}
	
	public PACAgent getParentAgent() {
		return parent;
	}

}
